const EventClass = require("yindu-event");
const toString   = Object.prototype.toString;
const AUDIOTYPES = ["mp3","wav","ogg"];
const AUDIOREGX  = new RegExp(".("+AUDIOTYPES.join("|")+")(\\??.*)$","ig");
const VIDEOTYPES = ["mp4","m3u8"];
const VIDEOREGX = new RegExp(".("+VIDEOTYPES.join("|")+")(\\??.*)$","ig");
const TYPES = {} ; 
["Null","String","Array","Number","Undefined","Object"].forEach(type => {
	TYPES[ `is${type}`] =val => toString.call(val) == `[object ${type}]`;
});
class YinduMedia extends EventClass {
	constructor({media,autoplay = false,loop=true,controller = document.querySelector(".ctrl")} = {}){
		super();
		this.loop = loop;
		this.autoplay = autoplay;
		this.controller = controller;
		let m = null;
		if(TYPES.isString(media)) {
			if(AUDIOREGX.test(media)) {
				m = document.createElement("audio");
				m.src = media;
				this.type = "audio";
			}else if(VIDEOREGX.test(media)){
				m = document.createElement("video");
				m.src = media;
				this.type = "video";
			}else {
				throw new TypeError("media must been the src or node type of audio/video,src:"+VIDEOTYPES+","+AUDIOTYPES);
			}
		}else if (media.nodeType == 1 && (media instanceof HTMLVideoElement || media instanceof HTMLAudioElement)){
			if(media instanceof HTMLVideoElement){
				m = document.createElement("video");
				m.src = media.src;
				this.type = "video";
			}else {
				m = document.createElement("audio");
				m.src = media.src;
				this.type = "audio";
			}
		}else {
			throw new TypeError("media must been the src or node type of audio/video,src:"+VIDEOTYPES+","+AUDIOTYPES);
		}
    
		if(!controller && this.type =="audio"){
			let controller = document.createElement("div");
			controller.classList && controller.classList.add("ctrl");
			document.body.appendChild(controller);
			this.controller = controller;
		}
		this.media =  m;
		this.media.loop = loop;
		this.media.autoplay = autoplay;
		this.init();
	}
  
	init () {
		let {media,controller} = this,self = this ;
		this.trigger("media:loadStart");
		media.addEventListener("timeupdate",function fn(){
			if(this.currentTime > 0 ) {
				self.trigger("media:loadComplete");
				media.removeEventListener("timeupdate",fn,!1);
			}
		},!1);
		media.addEventListener("play",function(){
			self.trigger("media:play");
			if(controller && controller.nodeType ==1){
				controller.classList && controller.classList.add("on");
			}
		},!1);
		media.addEventListener("pause",function(){
			self.trigger("media:pause");
			if(controller && controller.nodeType ==1){
				controller.classList && controller.classList.remove("on");
			}
		},!1);
		media.addEventListener("ended",function(){
			self.trigger("media:ended");
		},false);
		document.addEventListener("WeixinJSBridgeReady",() => {
			if(this.autoplay) {
				this.play();
			}
		},!1);
    
		if(controller && controller.nodeType == 1){
			controller.addEventListener("click",() => {
				if(media.paused) {
					this.play();
				}else {
					this.pause();
				}
			},false);
		}

	}
	play () {
		this.media.play();
	}
	pause () {
		this.media.pause();
	}
	resume() {
		this.play();
	}
	stop () {
		this.pause();
		this.media.currentTime = 0 ;
	}
  
}
module.exports = YinduMedia